<?php

require_once 'Core/Db/Table/Abstract.php';
require_once 'Core/Db/Table/Row/Abstract.php';
require_once 'Core/Db/Table/Rowset/Abstract.php';

class Nld_Models_Navigate_DbTable extends Core_Db_Table_Abstract
{
  public function init()
  {
		$this->_name 					= 'navigate';
		$this->_primary 			= 'id';
		$this->_rowClass 			= 'Nld_Models_Navigate_Row';
		$this->_rowsetClass 	= 'Core_Db_Table_Rowset_Abstract';
  }
	public function getStatus($index = null)
  {
		$list = array(
			Core_Translate::_('Ngừng kích hoạt'), 
			Core_Translate::_('Kích hoạt'));
    return ($index === null)?$list:@$list[$index];
  }
	public function fetchChild($where = '`parent` is null', $whereex = null, $order = null)
	{
    $entries  = array();
    foreach($this->fetchAll(
			($whereex === null)?$where:$where.' AND '.$whereex, 
			($order === null)?array('order asc', 'id asc'):$order) as $entry)
		{
			$entries = array_merge(
									 $entries, 
									 array($entry->toArray()), 
									 $this->fetchChild('`parent` = "'.$entry->id.'"', $whereex, $order)
									 			->toArray());
		}
		return $this->getRowSet($entries);
	}
  public function getMaxOrder()
  {
    $entry = $this->getAdapter()->query('SELECT MAX(`order`) AS maxOrder FROM ' . $this->_name)->fetchObject();
    return ($entry == null) ? 0 : intval($entry->maxOrder);
  }
}